package com.winchain.provider;

import com.shell.bee.persistence.transaction.Transaction;
import com.shell.bee.persistence.transaction.TransactionUtil;
import com.winchain.basic.exception.ExtException;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevMsgSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevSmsSubscriber;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;

@RestController
public class PrefinancingExtProvider {
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private BillDctRevMsgSubscriber billDctRevMsgSubscriber;
	@Autowired
	private BillDctRevSmsSubscriber billDctRevSmsSubscriber;
	private Log logger = LogFactory.getLog(getClass());

	@RequestMapping({ "/prefinancing/batchJob" })
	public void prefinancingBatchJob(@RequestParam("workDate") String workDate) {
		this.logger.info("--------prefinancingBatchJob excute start--------");
		try {
			boolean exFlag = false;
			int successNo = 0;

			List<CbRecVo> cbRecList = this.cbRecService.selectBillForPrefinancing(workDate);
			this.logger.info("cbRecList size is " + cbRecList.size());
			for (final CbRecVo cbRec : cbRecList) {
				try {
					TransactionUtil.execute(new Transaction() {
						public void doInTransaction() {
							BatchBillStats<CbDct> stats = new BatchBillStats();

							CbOpt opt = new CbOpt();
							opt.setCurState("S013");
							opt.setOptCode("E002");
							opt.setRemark("��������������");

							TimeoutOperation timeoutOpt = new TimeoutOperation();
							timeoutOpt.setOptCode("E004");
							timeoutOpt.setRemark("��������������,������������");

							BatchBillEvent<String, CbDct> event = new BatchBillEvent();
							List<String> list = new ArrayList();
							list.add(cbRec.getBillId());
							event.setRecs(list);
							event.setStats(stats);
							event.setOperation(opt);
							event.setTimeoutOpt(timeoutOpt);

							Observable<BatchBillEvent<String, CbDct>> observers = Observable.just(event);

							observers.subscribe(PrefinancingExtProvider.this.billDctRevMsgSubscriber);

							observers.subscribe(PrefinancingExtProvider.this.billDctRevSmsSubscriber);
						}
					}, new int[0]);

					successNo += 1;
				} catch (Exception e) {
					exFlag = true;
					e.printStackTrace();
					this.logger.error("������������,��������������������������", e);
				}
			}
			this.logger.info("exFlag:" + exFlag + " successNo:" + successNo);
			if (exFlag) {
				throw new ExtException("��������������������������������������������");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error("������������������������", e);
			throw new ExtException(e.getMessage());
		}
		this.logger.info("--------prefinancingBatchJob excute end--------");
	}
}
